package com.example.mytransitiondemo.Property;

import android.animation.Animator;
import android.animation.AnimatorInflater;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.animation.ValueAnimator;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.Toast;
import com.example.mytransitiondemo.BaseActivity;
import com.example.mytransitiondemo.R;

public class PropertyActivity extends BaseActivity implements View.OnClickListener {

    private ImageView rectangle_red;
    private Switch isXml;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_property);

        rectangle_red = (ImageView) findViewById(R.id.rectangle_red);
        isXml = (Switch) findViewById(R.id.isXml);

        findViewById(R.id.translate).setOnClickListener(this);
        findViewById(R.id.rotate).setOnClickListener(this);
        findViewById(R.id.scale).setOnClickListener(this);
        findViewById(R.id.alpha).setOnClickListener(this);
        findViewById(R.id.set).setOnClickListener(this);
        findViewById(R.id.setWay2).setOnClickListener(this);//集合2 动画
        findViewById(R.id.translate2).setOnClickListener(this);
        findViewById(R.id.rotate2).setOnClickListener(this);
        findViewById(R.id.scale2).setOnClickListener(this);
        findViewById(R.id.alpha2).setOnClickListener(this);
        findViewById(R.id.set2).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.translate:

                if (isXml.isChecked()) {  // 如果选了
                    Animator anim = AnimatorInflater.loadAnimator(this, R.animator.value_translate);
                    anim.setTarget(rectangle_red);
                    anim.start();
                    return;
                }
               // 没有选，就采用
                ObjectAnimator.ofFloat(rectangle_red, "translationX", 0.0F, 150.0F)
                        .setDuration(1000)
                        .start();
                break;
            case R.id.rotate:

                if (isXml.isChecked()) {
                    Animator anim = AnimatorInflater.loadAnimator(this, R.animator.value_rotate);
                    anim.setTarget(rectangle_red);
                    anim.start();
                    return;
                }

                ObjectAnimator.ofFloat(rectangle_red, "rotationX", 0.0F, 360.0F)
                        .setDuration(1000)
                        .start();
                break;
            case R.id.scale:

                if (isXml.isChecked()) {
                    Animator anim = AnimatorInflater.loadAnimator(this, R.animator.value_scale);
                    anim.setTarget(rectangle_red);
                    anim.start();
                    return;
                }

                ObjectAnimator.ofFloat(rectangle_red, "scaleX", 1.0F, 3.0F)
                        .setDuration(1000)
                        .start();
                break;
            case R.id.alpha:

                if (isXml.isChecked()) {
                    Toast.makeText(this, "这个不行", Toast.LENGTH_SHORT).show();
                    return;
                }

                ObjectAnimator.ofFloat(rectangle_red, "alpha", 1F, 0.5F)
                        .setDuration(1000)
                        .start();
                break;
            case R.id.set:
                //动画集合
                if (isXml.isChecked()) {
                    Animator anim = AnimatorInflater.loadAnimator(this, R.animator.value_set);
                    anim.setTarget(rectangle_red);
                    anim.start();
                    return;
                }

                PropertyValuesHolder pvhScaleX = PropertyValuesHolder.ofFloat("scaleX", 1f, 0f, 1f);
                PropertyValuesHolder pvhScaleY = PropertyValuesHolder.ofFloat("scaleY", 1f, 0f, 1f);
                ObjectAnimator customAnim = ObjectAnimator.ofPropertyValuesHolder(rectangle_red, pvhScaleX, pvhScaleY);
                customAnim.setDuration(4000);
                customAnim.start();

                break;
            case R.id.setWay2:
                //动画集合2
                if (isXml.isChecked()) {
                    Animator anim = AnimatorInflater.loadAnimator(this, R.animator.value_set);
                    anim.setTarget(rectangle_red);
                    anim.start();
                    return;
                }

                ObjectAnimator objectAnimator1 = ObjectAnimator.ofFloat(rectangle_red, "translationX", 0.0F, 150.0F).setDuration(1000);
                ObjectAnimator objectAnimator2 = ObjectAnimator.ofFloat(rectangle_red, "translationY", 0.0F, 150.0F).setDuration(1000);
                ObjectAnimator objectAnimator3 = ObjectAnimator.ofFloat(rectangle_red, "rotationX", 0.0F, 360.0F).setDuration(1000);
                AnimatorSet animatorSet = new AnimatorSet();
                animatorSet.setDuration(3000);
                // 随机设置
                animatorSet.play(objectAnimator1)
                        .with(objectAnimator2)
                        .after(objectAnimator3);
//                // 一起播放
//                animatorSet.playTogether(objectAnimator1, objectAnimator2, objectAnimator3);
//                // 顺序播放
//                animatorSet.playSequentially(objectAnimator1, objectAnimator2, objectAnimator3);
                animatorSet.start();
                break;
            case R.id.translate2:
                startValueAnimator(0);
                break;
            case R.id.rotate2:
                startValueAnimator(1);
                break;
            case R.id.scale2:
                startValueAnimator(2);
                break;
            case R.id.alpha2:
                startValueAnimator(3);
                break;
            case R.id.set2:
                startValueAnimator(4);
                break;
        }
    }


    /**
     * ValueAnimator
     */
    public void startValueAnimator(final int id) {
        ValueAnimator valueAnimator = ValueAnimator.ofFloat(150)
                .setDuration(1000);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                if (id == 0) {
                    rectangle_red.setTranslationX((Float) animation.getAnimatedValue());
                } else if (id == 1) {
                    rectangle_red.setRotation((Float) animation.getAnimatedValue());
                } else if (id == 2) {
                    rectangle_red.setScaleX((Float) animation.getAnimatedValue());
                } else if (id == 3) {
                    rectangle_red.setAlpha((Float) animation.getAnimatedValue());
                } else if (id == 4) {
                    rectangle_red.setTranslationX((Float) animation.getAnimatedValue());
                    rectangle_red.setScaleX((Float) animation.getAnimatedValue());
                    rectangle_red.setAlpha((Float) animation.getAnimatedValue());
                }
            }
        });
        valueAnimator.start();
    }
}
